var FeeType = {

    selected_row_id: null,
    objGrid: null,

    init: function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url: $('#table-url').val(),
            dataType: 'json',
            colModel: [
                {display: 'ID', name: 'id', width: 20, sortable: true, align: 'left', hide: false},
                {display: 'Fee Category', name: 'fee_category', width: 130, sortable: true, align: 'center', hide: false, editable: {form: 'select', validate: '', defval: '', options: fee_category}},
                {display: 'Description', name: 'description', width: 150, sortable: true, align: 'center', hide: false, editable: {form: 'text', validate: 'empty', defval: ''}},
                {display: 'Fee Code', name: 'fee_code', width: 60, sortable: true, align: 'center', hide: false, editable: {form: 'text', validate: 'empty', defval: ''}},
                {display: 'Bank Account', name: 'account_no', width: 100, sortable: true, align: 'center', hide: false, editable: {form: 'text', validate: 'empty,integer', defval: ''}},
                {display: 'Bank', name: 'bank_id', width: 100, sortable: false, align: 'center', hide: false, editable: {form: 'select', validate: '', bclass: 'bank-class', defval: '', options: banks_option}},
                {display: 'Bank Branch', name: 'bank_branch_id', width: 120, sortable: false, align: 'center', hide: false, editable: {form: 'select', validate: 'empty', defval: '', options: branches}},
                {display: 'Initial Charge', name: 'initial_charge', width: 100, align: 'center', editable: {form: 'text', validate: 'empty,decimalNumber', defval: ''}},
                {display: 'Additional Charge', name: 'additional_charge', width: 100, align: 'center', editable: {form: 'text', validate: '', defval: ''}},
                {display: 'Quantity Limit', name: 'quantity_limit', width: 80, sortable: false, align: 'center', hide: false, editable: {form: 'select', validate: '', defval: '', options: quantity_limits}}
            ],
            formFields: [
                {type: 'buttom', name: 'New', bclass: 'add', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Save', bclass: 'save', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Edit', bclass: 'edit', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Cancel', bclass: 'cancel', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Delete', bclass: 'delete', onpress: self.handleGridEvent}
            ],
            /*searchitems:[
             {display:'Proforma Number', name:'invoice_number', isdefault:true}
             ],*/
            checkboxSelection: true,
            editablegrid: {
                use: true,
                url: $('#table-editable-url').val()
            },
            columnControl: false,
            sortname: "id",
            sortorder: "desc",
            usepager: true,
            useRp: true,
            rp: 50,
            showTableToggleBtn: false,
            height: 300,
            subgrid: {
                use: false
            }
        });


        $(".bank-class").live('change', function () {
            var sel = $(this);
            var value = sel.val();
            var row_id = sel.parent().parent().parent().attr('data-id');
            var select = document.getElementById('bank_branch_id_' + row_id);
            select.options.length = 0;
            if (typeof bank_branches[value] == "undefined") {
                var opt = document.createElement('option');
                opt.value = '';
                opt.text = '';
                try { //Standard
                    select.add(opt, null);
                }
                catch (error) { //IE Only
                    select.add(opt);
                }
                return;
            }
            var my_branches = bank_branches[value];
            var d_options = my_branches;

            for (var nx in d_options) {
                var k = d_options[nx]['id'];
                var name = d_options[nx]['name'];
                var opt = document.createElement('option');
                opt.value = k;
                opt.text = name;
                try { //Standard
                    select.add(opt, null);
                }
                catch (error) { //IE Only
                    select.add(opt);
                }
            }
        });

    },

    handleGridEvent: function (com, grid, json) {
        if (com == 'New') {
            FeeType.objGrid.flexBeginAdd();
        }
        else if (com == 'Edit') {
            var row = FlexObject.getSelectedRows(grid);
            FeeType.objGrid.flexBeginEdit(row[0]);
        }
        else if (com == 'Save') {
            FeeType.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            FeeType.objGrid.flexCancel();
        }
        else if (com == 'Delete') {
            if (FlexObject.rowSelectedCheck(FeeType.objGrid, grid)) {
                FeeType.delete_(grid);
            }
        }
    },

    delete_: function (grid) {
        var self = this;
        var url = $('#delete_url').val();
        jLib.do_delete(url, grid);
    }
};

/* when the page is loaded */
$(document).ready(function () {
    FeeType.init();
});